<?php

 /**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */




class Database_Conn {
	


/**
 * @access private
 * @var integer
 */
    private  $_dbConnection;

/**
 * @access private
 * @var object
 */
    private $_stmt;

 
 /**
  * Constructor sets up {@link $_dbConnection}
  */
    public function __construct() {

        $this->_dbConnection = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_DBNAME);
        if (mysqli_connect_errno()) {

            throw new exception(mysqli_connect_error(), mysqli_connect_errno());

        }
    }//EOF __construct
/**
  * Bind sql statement with bind parameters and sets up {@link $_stmt}
  * @param string $sql It is a sql query
  * @param array $arrParams It is a array of bind parameters.
  */
    public function prepareSql($sql,$arrParams= ''){

        $this->_stmt = $this->_dbConnection->prepare($sql);
        if (!$this->_stmt) {

            throw new exception(mysqli_error($this->_dbConnection), mysqli_errno($this->_dbConnection)); 

        } 
        if($arrParams !=''){

			$tmp = array();
            foreach($arrParams as $key => $value) $tmp[$key] = &$arrParams[$key];

            if(!call_user_func_array(array($this->_stmt, 'bind_param'), $tmp)){

                throw new exception(mysqli_error($this->_dbConnection), mysqli_errno($this->_dbConnection)); 

            }//EO if

        }//EO if
        if(!$this->_stmt->execute()) {

            throw new exception(mysqli_error($this->_dbConnection), mysqli_errno($this->_dbConnection));

        }//EO if

    }//EOF

 /**
 * It returns Last inserted id  
 * @return integer $_stmt->insert_id Last inserted Id.
 */
    public function getLastInsertId(){

        return $this->_stmt->insert_id;

    }//EOF

    

/**
 * It returns Resultset of Select query.  
 * @return array $arrResult.
 */
    public function getResults(){

        $meta = $this->_stmt->result_metadata(); 
        while ($field = $meta->fetch_field()) 
        { 
            $params[] = &$row[$field->name]; 
        }//EO while

        if(!call_user_func_array(array($this->_stmt, 'bind_result'), $params) ){

            throw new exception(mysqli_error($this->_dbConnection), mysqli_errno($this->_dbConnection)); 

        }//EO if

        $arrResult = array();
        while ($this->_stmt->fetch()) { 
            foreach($row as $key => $val) 
            {
                $c[$key] = utf8_decode(trim($val)); 
            }//EO foreach

            $arrResult[] = $c;

        }//EO while

        return $arrResult;

    }//EOF

    

/**
 * It commits the transaction.  
 */
    public function commitTransactions(){

        $this->_dbConnection->commit();

    }

/**
 * It rollbacks the transaction.  
 */
    public function rollBackTransactions(){

        $this->_dbConnection->rollback();
    }

/**
 * It closes the statement object.  
 */
    public function closeStmt() {

        $this->_stmt->close();

    }

/**
 * It closes the database connection object.  
 */
    public function closeConn() {

        $this->_dbConnection->close();

    }
/**
 * It makes autocommit on or off
 * @param boolean $autoCommit.  
 */
    public function beginTransaction($autoCommit) {
        $this->_dbConnection->autocommit($autoCommit);
    }
/**
 * It closes $_stmt and $_dbConnection objects.
 */
    public function __destruct() {
        if($this->_stmt) {
            $this->closeStmt();
        }
        $this->closeConn();
    }

}//EO class
?>